<!DOCTYPE html>
<%@page import="java.util.UUID"%>
<%@page import="java.io.File"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="java.util.List"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="java.io.InputStream"%>
<html lang="en">
<head>
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>在此处插入标题</title>
</head>
<body>
<%-- <%
 request.setCharacterEncoding("utf-8");
String sname=request.getParameter("sname");
String pic=request.getParameter("idcardpic");
%>
<%=sname %>,<%=pic %> --%>
<%-- <%
 InputStream in=request.getInputStream();
 int ch=0;
 while((ch=in.read())!=-1){
	 out.print((char)ch);
 }
%> --%>
<%
	//request.setCharacterEncoding("utf-8")无效
	DiskFileItemFactory factory=new DiskFileItemFactory();
	factory.setSizeThreshold(1024*1024);//缓存大小
	ServletFileUpload upload=new ServletFileUpload(factory);
	upload.setHeaderEncoding("utf-8");
	List<FileItem> list=upload.parseRequest(request);
	for(FileItem f:list){
%>
 <p>
  <% if(f.isFormField()) {
  	out.print("这是表单元素："+f.getFieldName()+",值是："+f.getString("utf-8"));
  }else{
	  out.print("这是文件元素");
	  if(f.getSize()==0)//没有选择文件
		  continue;
  	  out.print(" 文件名："+f.getName()+",元素名："+f.getFieldName());	
 	 
  	  File path=new File(application.getRealPath("img"));
  	  if(path.exists()==false)
  		  path.mkdirs();
  	  String suffix=f.getName().substring(f.getName().lastIndexOf('.'));
  	  String newName=UUID.randomUUID()+suffix;
  	  f.write(new File(path,newName));
  }
  %>
 </p>
  <%} %>
</body>
</html>